//
//  UIExtensionHeader.swift
//  SwiftStudy
//
//  Created by chang on 2018/2/2.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit

extension UIViewController {
    /**
     * creat alert controller
     *
     * @param aStyle  actionSheet 和 alert
     *
     * @param aStr,bStr,aCancel 都是选项参数标题
     */
    func showAlert(aStyle:UIAlertControllerStyle,aTitle:String,aMessage:String,aStr:String,bStr:String,aCancel:String,completion: ((Int) -> Swift.Void)? = nil  ) -> () {
        let alertController = UIAlertController(title:aTitle, message:aMessage, preferredStyle:aStyle)
        
        if aStr.lengthOfBytes(using:.utf8)>0 {
            let aSureAction = UIAlertAction.init(title: aStr, style: .default, handler: {
                (UIAlertAction) in
                completion?(0)
            })
            alertController.addAction(aSureAction)
        }
        
        if bStr.lengthOfBytes(using:.utf8)>0 {
            let aSureAction = UIAlertAction.init(title: bStr, style: .default, handler: {
                (UIAlertAction) in
                completion?(1)
            })
            alertController.addAction(aSureAction)
        }
        
        if aCancel.lengthOfBytes(using: .utf8)>0 {
            let aCancelAction = UIAlertAction.init(title: aCancel, style: .cancel, handler: {
                (UIAlertAction) in
                completion?(2)
            })
            alertController.addAction(aCancelAction)
        }
        self.present(alertController, animated: true, completion: nil)
    }
}

extension NSObject {
    //通过类名字符串获取类对象
    func swiftClassFromString(className: String) -> UIViewController! {
        //get the project name
        if let appName : String = Bundle.main.object(forInfoDictionaryKey: "CFBundleName") as? String {
            //拼接控制器名
            let classStringName = "\(appName).\(className)"
            //将控制名转换为类
            let classType = NSClassFromString(classStringName) as? UIViewController.Type
            if let type = classType {
                let newVC = type.init()
                return newVC
            }
        }
        return nil;
    }
}



